package action.base;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.User;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Controller
@Scope("prototype")
public class BaseAction extends ActionSupport implements ServletRequestAware,
		ServletResponseAware, SessionAware, ServletContextAware {

	private static final long serialVersionUID = 6510770407656025666L;
	protected Logger log = Logger.getLogger(this.getClass());
	private HttpServletRequest request;
	private HttpServletResponse response;
	private ServletContext context;
	private Map<String, Object> session;

	public User getUser() {
		ActionContext context = ActionContext.getContext();

		if (context.getSession() != null) {

			User user = (User) context.getSession().get("user");
			if (user != null) {
				return user;
			} else
				return null;
		} else
			return null;
	}

	public String getUserName() {
		ActionContext context = ActionContext.getContext();
		final int error = -1;
		if (context.getSession() != null) {

			User user = (User) context.getSession().get("user");
			if (user != null) {
				return user.getName();
			} else
				return null;
		} else
			return null;
	}

	public String[] getParameterStrA(String key) {
		ActionContext actioncontext = ActionContext.getContext();
		Map map = actioncontext.getParameters();
		return (String[]) map.get(key);
	}

	public Map getParameterMap(String key) {
		ActionContext actioncontext = ActionContext.getContext();
		Map map = actioncontext.getParameters();
		return (Map) map.get(key);
	}

	public String getParameterStr(String key) {
		String[] stra = getParameterStrA(key);
		if (stra != null && stra.length > 0)
			return stra[0];
		else
			return null;
	}

	public Integer getParameterInt(String key) {
		String[] stra = getParameterStrA(key);
		if (stra != null && stra.length > 0)
			return Integer.parseInt(stra[0]);
		else
			return null;
	}

	public String getRealPath() {
		return context.getRealPath("/");
	}

	@Override
	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		this.request = arg0;
	}

	@Override
	public void setServletContext(ServletContext arg0) {
		this.context = arg0;
	}

	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		this.response = arg0;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public ServletContext getContext() {
		return context;
	}

}